package handler

import (
	"context"
	"fmt"
	"math/rand"
	"server/basic/global"
	"server/basic/inits"
	"server/models"
	__ "server/proto"
	"time"
	"unicode"
)

func (s *Server) Sendsms(_ context.Context, in *__.SendsmsRequest) (*__.SendsmsResponse, error) {
	code := rand.Intn(9000) + 1000
	//sendsms, err := pkg.Sendsms(in.Tel, strconv.Itoa(code))
	//if err != nil {
	//	return nil, err
	//}
	//if *sendsms.Body.Message != "OK" {
	//	return nil, fmt.Errorf(*sendsms.Body.Code)
	//}
	inits.RedisClient.Set(context.Background(), "sendsms"+in.Tel+in.Score, code, time.Minute*6)
	return &__.SendsmsResponse{}, nil
}
func IsNum(s string) bool {
	for _, i := range s {
		if !unicode.IsDigit(i) {
			return false
		}
	}
	return true
}
func (s *Server) Register(_ context.Context, in *__.RegisterRequest) (*__.RegisterResponse, error) {
	if len(in.Tel) != 11 || !IsNum(in.Tel) {
		return nil, fmt.Errorf("号码格式不正确")
	}
	var user models.Users
	global.DB.Where("tel = ?", in.Tel).Find(&user)
	if user.Tel != "" {
		return nil, fmt.Errorf("用户已存在 请去登录")
	}
	//get := inits.RedisClient.Get(context.Background(), "sendsms"+in.Tel+"register")
	//if get.Val() != in.Sendsms {
	//	return nil, fmt.Errorf("验证码错误")
	//}
	//err := inits.RedisClient.Del(context.Background(), "sendsms"+in.Tel+"register").Err()
	//if err != nil {
	//	return nil, fmt.Errorf("验证码已过期")
	//}
	add := models.Users{
		Tel:      in.Tel,
		Password: in.Password,
	}
	global.DB.Create(&add)

	return &__.RegisterResponse{
		Id: add.Id,
	}, nil
}
func (s *Server) Login(_ context.Context, in *__.LoginRequest) (*__.LoginResponse, error) {
	if len(in.Tel) != 11 || !IsNum(in.Tel) {
		return nil, fmt.Errorf("号码格式不正确")
	}
	var user models.Users
	global.DB.Where("tel = ?", in.Tel).Find(&user)
	if user.Tel == "" {
		return nil, fmt.Errorf("用户不存在 请先去注册")
	}
	if user.Password != in.Password || user.Tel != in.Tel {
		return nil, fmt.Errorf("账号或密码错误")
	}
	return &__.LoginResponse{
		Id: user.Id,
	}, nil
}
func (s *Server) List(_ context.Context, in *__.ListRequest) (*__.ListResponse, error) {
	var user []models.Exams
	global.DB.Find(&user)
	var exams []*__.Exam
	for _, v := range user {
		lists := &__.Exam{
			Type:             v.Type,
			ExamName:         v.ExamName,
			ExamType:         v.ExamType,
			ExamOrder:        v.ExamOrder,
			OptionOrder:      v.OptionOrder,
			ExamScore:        v.ExamScore,
			PassingScore:     v.PassingScore,
			ExamInstructions: v.ExamInstructions,
			Examiner:         v.Examiner,
		}
		exams = append(exams, lists)
	}
	return &__.ListResponse{
		List: exams,
	}, nil
}
func (s *Server) Time(_ context.Context, in *__.TimeRequest) (*__.TimeResponse, error) {
	var exam models.Exams
	global.DB.Where("id= ?", in.Id).First(&exam)
	//if exam.Id == 0 {
	//	return nil, fmt.Errorf("当前考试不存在")
	//}
	//if time.Now().Before(exam.ExamStrartime) {
	//	return nil, fmt.Errorf("考试未开始")
	//}
	if time.Now().After(exam.ExamEndtime) {
		return nil, fmt.Errorf("您已超过入场时间 已缺考")
	}
	if time.Now().After(exam.ExamEndtime.Add(5 * time.Minute)) {
		update := models.Exams{
			Status: in.Status}
		err := global.DB.Updates(&update).Error
		if err != nil {
			return nil, fmt.Errorf("更新考试状态失败")
		}
		return nil, fmt.Errorf("您已超过入场时间 已缺考")
	}

	return &__.TimeResponse{}, nil
}
func (s *Server) Answer(_ context.Context, in *__.AnswerRequest) (*__.AnswerResponse, error) {
	var exam models.Qusetions
	global.DB.Where("name= ?", in.Name).First(&exam)
	//if exam.Name == "" {
	//	return nil, fmt.Errorf("题目不存在 不能答题")
	//}
	add := models.Qusetions{
		Name:   in.Name,
		Answer: in.Answer,
	}
	err := global.DB.Create(&add).Error
	if err != nil {
		return nil, fmt.Errorf("试卷提交失败")
	}

	return &__.AnswerResponse{
		Id: int64(add.Id),
	}, nil
}
